/*define calendar popup list*/
function setupCalendar(calendarList) {
    for (var i = 0; i < calendarList.length; i++) {
        var item = calendarList[i][0];
        var btn = calendarList[i][1];
        Calendar.setup(
        {
            inputField     :    item,
            ifFormat       :    "%d/%m/%Y",
            showsTime      :    false,
            button         :    btn
        });
    }
}

/*validate date format on onblur event*/
function date_onblur(item) {
    if (isValidDateDDMMYYYY(item.value)) {
        window.status = "";
        var str = new String(item.value);
        var strDisplayDate = "";

        //var current_date = getCurentBusinessDate();
        //var str2 = new String(current_date.getFullYear());
        var str2 = '2500';

        if (str.length == 8) {
            if (str.indexOf('/', 0) >= 0) {
                var dateSep = /(\/)/;
                var datePart = str.split(dateSep);
                strDisplayDate = datePart[0] + '/' + datePart[1] + '/' + str2.substring(0, 2).concat(datePart[2]);
            }
            else {
                strDisplayDate = str.substring(0, 2) + '/' + str.substring(2, 4) + '/' + str.substring(4);
            }

            item.value = strDisplayDate;
        }
        if (str.length == 6) {
            strDisplayDate = str.substring(0, 2) + '/' + str.substring(2, 4) + '/' + str2.substring(0, 2).concat(str.substring(4, 6));
            item.value = strDisplayDate;
        }

        var strDate = item.value;

        var datepart = strDate.split('/');
        var dateValue = new Date(datepart[2], datepart[1], datepart[0], 0, 0, 0);

        var calType = Calendar._TT['CAL_TYPE'];
        var minDate = Calendar._TT['MIN_DATETIME'];
        var maxDate = Calendar._TT['MAX_DATETIME'];
        if (dateValue.getTime() < minDate.getTime() || dateValue.getTime() > maxDate.getTime()) {
            item.value = "";
            alert('Select Date is out of Range');
            return false;
        } else {
            return true;
        }
    } else {
        if (item.value != "") {
            window.status = "Wrong Date Format, Required DD/MM/YY , DD/MM/YYYY, DDMMYY, DDMMYYYY";
        }
        item.value = "";
        return false;
    }
}

/*validate date format on mouse out event*/
function date_onmouseout(item) {
    if (isValidDateDDMMYYYY(item.value)) {
        window.status = "";

        var str = new String(item.value);
        var strDisplayDate = "";

        //var current_date = getCurentBusinessDate();
        //var str2 = new String(current_date.getFullYear());
		var str2 = '2500';
		
        if (str.length == 8) {
            if (str.indexOf('/', 0) >= 0) {
                var dateSep = /(\/)/;
                var datePart = str.split(dateSep);
                strDisplayDate = datePart[0] + '/' + datePart[1] + '/' + str2.substring(0, 2).concat(datePart[2]);
            } else {
                strDisplayDate = str.substring(0, 2) + '/' + str.substring(2, 4) + '/' + str.substring(4, 8);
            }
            item.value = strDisplayDate;
        }
        if (str.length == 6) {
            strDisplayDate = str.substring(0, 2) + '/' + str.substring(2, 4) + '/' + str2.substring(0, 2).concat(str.substring(4, 6));
            item.value = strDisplayDate;
        }

        var strDate = item.value;

        var datepart = strDate.split('/');
        var dateValue = new Date(datepart[2], datepart[1], datepart[0], 0, 0, 0);

        var calType = Calendar._TT['CAL_TYPE'];
        var minDate = Calendar._TT['MIN_DATETIME'];
        var maxDate = Calendar._TT['MAX_DATETIME'];
        if (dateValue.getTime() < minDate.getTime() || dateValue.getTime() > maxDate.getTime()) {
            item.value = "";
            alert('Select Date is out of Range');
            return false;
        } else {
            return true;
        }
    } else {
        if (item.value != "") {
            window.status = "Wrong Date Format, Valid only : DD/MM/YY , DD/MM/YYYY, DDMMYY, DDMMYYYY";
        }
        item.value = "";
        return false;
    }
}

/*validate date format on key press event*/
function date_onkeypress() {
    var date_Pattern = /[0-9\/]/i;
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    if (date_Pattern.test(character)) {
        return true;
    } else {
        return false;
    }
}

/*validate date format on key up event*/
function date_onkeyup(item) {
    var date_Pattern = /[0-9\/]/i;

    if (date_Pattern.test(item.value)) {
        window.status = "";
        return true;
    } else {
        if (item.value != "") {
            window.status = "Wrong Date Format, Valid only : DD/MM/YY , DD/MM/YYYY, DDMMYY, DDMMYYYY";
        }
        item.value = "";
        return false;
    }
}

/*validate date format ,corrected format is [DD/MM/YYYY , DD/MM/YY, DDMMYYYY, DDMMYY]*/
function isValidDateDDMMYYYY(strDate) {

    var date_Pattern_Leap = /^(((((([0-2][1-9])|([1-3][0-1]))\/(([0][1,3,5,7,8])|([1][0,2])))|((([0-2][1-9])|([1-3][0]))\/(([0][4,6,9])|([1][1])))|((([0-2][1-9])|([1,2][0]))\/(([0][2]))))\/(([1-9][0-9][0-9][0-9])|(([0-9][1-9])|([1-9][0]))))|((((([0-2][1-9])|([1-3][0-1]))(([0][1,3,5,7,8])|([1][0,2])))|((([0-2][1-9])|([1-3][0]))(([0][4,6,9])|([1][1])))|((([0-2][1-9])|([1,2][0]))(([0][2]))))(([1-9][0-9][0-9][0-9])|(([0-0][1-9])|([1-9][0])))))$/i;
    var date_Pattern_nonLeap = /^((((((([0-2][1-9])|([1-3][0-1]))\/(([0][1,3,5,7,8])|([1][0,2])))|((([0-2][1-9])|([1-3][0]))\/(([0][4,6,9])|([1][1])))|(((([0-1][1-9])|([2][0-8]))|([1,2][0]))\/(([0][2]))))\/(([1-9][0-9][0-9][0-9])|(([0-9][1-9])|([1-9][0])))))|((((([0-2][1-9])|([1-3][0-1]))(([0][1,3,5,7,8])|([1][0,2])))|((([0-2][1-9])|([1-3][0]))(([0][4,6,9])|([1][1])))|(((([0-1][1-9])|([2][1-8]))|([1,2][0]))(([0][2]))))(([1-9][0-9][0-9][0-9])|(([0-9][1-9])|([1-9][0])))))$/i;
    var date_Pattern = date_Pattern_Leap;
    var str = new String(strDate);

    //var current_date = getCurentBusinessDate();
   // var str2 = new String(current_date.getFullYear());
    var str2 = '2500';
    var strYear = '';
    var datePart;
    if (str.length == 8) {
        var dateSep = /(\/)/;
        if (str.indexOf('/', 0) >= 0) {
            datePart = str.split(dateSep);
            strYear = str2.substring(0, 2).concat(datePart[2]);
        } else {
            strYear = str.substring(4, 8);
        }
    } else if (str.length == 6) {
        strYear = str2.substring(0, 2).concat(str.substring(4, 6));
    } else if (str.length == 10) {
        var dateSep = /(\/)/;
        datepart = str.split('/');
        strYear = datepart[2];
    } else {
        return false;
    }
    
    strYear = parseInt(strYear) - 543;

    if (eval(strYear % 4) == 0) {
        date_Pattern = date_Pattern_Leap;
    } else {
        date_Pattern = date_Pattern_nonLeap;
    }
    
    
    if (str.length == 8) {
    	if (str.indexOf('/', 0) >= 0) {
    		strDate = str.substring(0,6).concat(strYear);
    	}else
    	{
    		strDate = str.substring(0,4).concat(strYear);
    	}
    }else if (str.length == 6) {
    	strDate = str.substring(0,4).concat(strYear);
    } else if (str.length == 10) {
    	strDate = str.substring(0,6).concat(strYear);
    }

    if (date_Pattern.test(strDate)) {
        return true;
    } else {
        return false;
    }
}

function compareDateString(strDateFrom, strDateTo) {
    var result = 0;
    var delim1 = "/";
    var delim2 = "/";
    if (trim(strDateFrom) != "" && trim(strDateTo) != "") {
        var dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
        var matchedFrom = dateRegexp.exec(strDateFrom);
        var matchedTo = dateRegexp.exec(strDateTo);
     //Thai Only
     	yearFrom = parseInt(matchedFrom[3])-543;
     	yearTo = parseInt(matchedTo[3])-543;
     // Warning
        var dateFrom = new Date(yearFrom, matchedFrom[2] - 1, matchedFrom[1], 0, 0, 0);
        var dateTo = new Date(yearTo, matchedTo[2] - 1, matchedTo[1], 0, 0, 0);
        var date1 = (dateFrom.getFullYear() * 10000) + ((dateFrom.getMonth()+1) * 100) + dateFrom.getDate();
        var date2 = (dateTo.getFullYear() * 10000) + ((dateTo.getMonth()+1) * 100) + dateTo.getDate();
        result = date1 - date2;
    }
    return result;
}

function getCurrentDate() {
    var myDate = getCurentBusinessDateTime();
    var day = myDate.getDate();
    var month = myDate.getMonth() + 1;
    var year = myDate.getFullYear();
    if (day <= 9) {
        day = "0" + day;
    }
    if (month <= 9) {
        month = "0" + month;
    }
    year = parseInt(year) + 543;
    return day + "/" + month + "/" + year;
}

function getCurentBusinessDateTime() {
    var curDateTimeDiv = document.getElementById("template-date-time");
    var rexDateTime = /(\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)/;
    var result = curDateTimeDiv.innerHTML.match(rexDateTime);

    var day = result[1];
    var month = result[2] - 1;
    var year = result[3];
    var hour = result[4];
    var min = result[5];
    var sec = result[6];
    //Thai only
    year = parseInt(year) - 543;
    var curDateTime = new Date (year, month, day, hour, min, sec);
    return curDateTime;
}